package com.cn.tianxin.ssserver.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class PropUtil {

    public static Map<String,PropUtil> GLOBAL_PROP_MAP = new HashMap<>();

    private Properties properties = new Properties();
    private PropUtil(String propertyFileName) throws IOException {
        InputStream inputStream = PropUtil.class.getClassLoader().getResourceAsStream(propertyFileName);
        properties = new Properties();
        properties.load(inputStream);
    }

    private PropUtil(File propertyFile) throws IOException {
        InputStream inputStream = new FileInputStream(propertyFile);
        properties = new Properties();
        properties.load(inputStream);
    }

    public static PropUtil getPropUtilFromMap(String fileName) throws IOException {
        PropUtil propUtil = GLOBAL_PROP_MAP.get(fileName);
        if (propUtil!=null){
            return propUtil;
        }else {
            propUtil = new PropUtil(fileName);
            GLOBAL_PROP_MAP.put(fileName,propUtil);
            return propUtil;
        }
    }

    //获取配置文件的属性值
    public String get(String key){
        return properties.getProperty(key);
    }

}
